package makabakaquestionnaire.zutsoftware.framework.aspect;

import makabakaquestionnaire.zutsoftware.annotation.DataScope;
import makabakaquestionnaire.zutsoftware.entity.BaseEntity;
import makabakaquestionnaire.zutsoftware.entity.Users;
import makabakaquestionnaire.zutsoftware.framework.security.model.LoginUser;
import makabakaquestionnaire.zutsoftware.framework.security.utils.SecurityUtils;
import makabakaquestionnaire.zutsoftware.system.mapper.SysUserRoleMapper;
import makabakaquestionnaire.zutsoftware.utils.String.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DataScopeAspect {
    @Autowired
    public SysUserRoleMapper userRoleMapper;
    /**
     * 全部数据权限
     */
    public static final String DATA_SCOPE_ALL = "1";

    /**
     * 自定数据权限
     */
    public static final String DATA_SCOPE_CUSTOM = "2";

    /**
     * 部门数据权限
     */
    public static final String DATA_SCOPE_DEPT = "3";

    /**
     * 部门及以下数据权限
     */
    public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";

    /**
     * 仅本人数据权限
     */
    public static final String DATA_SCOPE_SELF = "5";

    /**
     * 数据权限过滤关键字
     */
    public static final String DATA_SCOPE = "dataScope";

    /**
     * 在含有@DataScope的注解前做切面处理
     * @param point
     * @param dataScope
     */
    @Before("@annotation(dataScope)")
    public void Before(JoinPoint point, DataScope dataScope){
        System.out.println("-----------------DataScope切面开始");
        handleDataScope(point, dataScope);
        System.out.println("-----------------DataScope切面完成");
        TestJointPoint(point);

    }
    public void TestJointPoint(JoinPoint joinPoint){
        Object target = joinPoint.getTarget();
        System.out.println("切入点所在目标对象"+target.getClass().getName());
        String methodName = joinPoint.getSignature().getName();
        System.out.println("切入点方法名称"+methodName);
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        System.out.println("切入点GetMethod"+signature.getMethod());
        System.out.println("切入点注解"+signature.getMethod().getAnnotation(DataScope.class));


        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println("切入点方法的参数"+arg);
        }
    }


    public void handleDataScope(final JoinPoint joinPoint, DataScope controllerDataScope)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            Users currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(),
                        controllerDataScope.userAlias());
            }
        }
    }

    public  static  void dataScopeFilter(JoinPoint joinPoint, Users users,String deptAlias,String userAlias){
        StringBuilder sqlString = new StringBuilder();

//        List<SysRole> roles = users.getRoles();
//        for (SysRole role : roles) {
//            String dataScope = role.getDataScope();
//            if (DATA_SCOPE_SELF.equals(dataScope)){
//                sqlString.append(StringUtils.format(" OR {}.user_id = {}",userAlias,users.getId()));
//            }
//        }
        sqlString.append(StringUtils.format(" OR {}.id = {}",userAlias,users.getId()));
        if (StringUtils.isNotEmpty(sqlString.toString())){
            Object params = joinPoint.getArgs()[0];
            if (params!=null &&params instanceof BaseEntity){
                BaseEntity baseEntity = (BaseEntity) params;
                baseEntity.getParams().put(DATA_SCOPE," AND ("+sqlString.substring(4)+")");
            }
        }

    }




}
